<?php

require(__DIR__ . "/page_manager.php");

/**
 * Shows fatal error and terminate script
 *
 * @param string $error
 * Add Error to stack
 */
function fatal_error($error) {
    stack_errors($error);
    die;
}

/**
 * Stack error
 *
 * @param string $error
 * Add error to stack
 */
function stack_errors($error) {
    print "<table width=100% border=1 bgcolor=#FFFFFF>\n";
    print "<tr width=100%>\n";
    print "<td width=100%>\n";
    print "<font color=red>ERROR: </font>{$error}\n";
    print "</td>\n";
    print "</tr>\n";
    print "</table>\n";
}

class base {

    public $db;
    private $tags;
    public $requests;
    public $settings;
    public $page_manager = null;
    public $users_array;
    public $submenu;
    public $template_path = null;
    public $webpage_name = null;
    public $logo_image = null;
    public $nosession = false;    // do not handle logins/sessions!

    /**
     * Get global requests
     */

    public function get_request() {
        $this->requests = $_REQUEST;
    }

    /**
     * Get submenus
     *
     * @param string $submenu
     * Add submenu to submenus
     */
    public function _submenu($submenu) {
        $this->page_manager->submenu($submenu);
    }

    /**
     * Run web, attach menus to functions
     *
     * @return string
     * Callback function
     */
    public function _run() {
        $submenu = $_REQUEST["submenu"];
        $submenus = $this->submenu;
        $this->page_manager->page = $submenus[0]["name"];
        $callback = false;

        foreach ($submenus as $submenu_def) {
            if (strtolower($submenu_def["name"]) == strtolower($submenu)) {
                $callback = $submenu_def["callback"];
            }
        }

        if (!$callback) {
            $default_subpage = $submenus[0]["name"];
            header("Location: index.php?page=index&submenu={$default_subpage}");
        }

        return $callback;
    }

    /**
     * Master finish
     *
     * @global string $script_name
     * @global string $script_version
     * @param string $content
     * Content to show on page
     */
    public function finish($content) {
        global $script_name, $script_version;
        $user = $this->current_user_info();
        $info = "User: {$user["name"]}<br>Code: {$script_name} {$script_version}<br>\n";
        $this->page_manager->finish_script($content, $info);
    }

    /**
     * Setup page variables and do some inits
     *
     * @param string $template
     * Template path
     * @param string $page
     * Page name
     * @param array $menus
     * Menus to show
     *
     */
    public function setup_page(&$template, &$page, &$menus) {
        $this->page_manager = new page_manager($template, $this->template_path, $this->webpage_name);
        $this->page_manager->logo_image = $this->logo_image;
        $this->page_manager->nosession = $this->nosession;
        $this->page_manager->load($page, $this->users_array);
        $this->page_manager->menus($menus);
        $this->page_manager->gzcompress = true;
        $this->page_manager->elements_embed = true;
    }

    /**
     * Logged in user info
     *
     * @return mixed
     * Return false if there is no user or user info if found
     */
    public function current_user_info() {
        $uid = $this->page_manager->getuserinfo();
        if (!$uid)
            return false;
        foreach ($this->users_array as $user => $attributes) {
            if ($attributes["id"] == $uid)
                $uinfo = array_merge(array("name" => $user), $attributes);
        }
        return $uinfo;
    }

    /**
     * Load users from source
     *
     * @param mixed $users_array
     * Loaded users array (you can load info from some other source), or if false, load from DB
     */
    public function load_users($users_array = false) {
        if ($users_array)
            $rows = $users_array;
        else
            $rows = $this->db->get_rows("users");
        if (empty($rows))
            stack_errors("No users found!");
        foreach ($rows as $row) {
            $id = $row["id"];
            $username = $row["username"];
            $password = $row["password"];
            $ulevel = (int) $row["ulevel"];
            $this->users_array[$username]["id"] = $id;
            $this->users_array[$username]["password"] = $password;
            $this->users_array[$username]["ulevel"] = $ulevel;
        }
    }

}

$page = $_REQUEST["page"];
if (empty($page))   // default page
    $page = "index";

$reqfile = __DIR__ . "/" . $page . ".php";
if (!file_exists($reqfile))
    fatal_error("Page template file {$reqfile} does not exist!");
else
    require($reqfile);

$instance = new $page;    // instance page extended from base class
$instance->settings = $set;
$instance->db = $db;
$instance->nosession = false;
$instance->template_path = "templates/sand/";
$instance->webpage_name = $webpage_name;
$instance->logo_image = $logo_image;

$instance->load_users($users_array);
$instance->setup_page($tmpl, $page, $menus);
$instance->run();
?>